package script.grammar.expression;

import script.grammar.symbol.NativeType;
import script.grammar.symbol.Type;

public class EqualityOperator extends BinaryOperator {
	private static Type[][] table = {
						//void				boolean				int					double				object
		/*void*/		{NativeType.VOID,	NativeType.VOID,	NativeType.VOID,	NativeType.VOID,	NativeType.VOID},
		/*boolean*/		{NativeType.VOID,	NativeType.BOOLEAN,	NativeType.VOID,	NativeType.VOID,	NativeType.VOID},
		/*int*/			{NativeType.VOID,	NativeType.VOID,	NativeType.BOOLEAN,	NativeType.BOOLEAN,	NativeType.VOID},
		/*double*/		{NativeType.VOID,	NativeType.VOID,	NativeType.BOOLEAN,	NativeType.BOOLEAN,	NativeType.VOID},
		/*object*/		{NativeType.VOID,	NativeType.VOID,	NativeType.VOID,	NativeType.VOID,	NativeType.OBJECT}
	};
	
	public EqualityOperator(String name) {
		super(name);
	}

	@Override
	protected Type computeTypeImpl(Type o1, Type o2) {
		Type val = table[o1.getIndex()][o2.getIndex()];
		if (val.equals(NativeType.OBJECT)) {
			if (o1.equals(o2)) {
				return NativeType.BOOLEAN;
			} else {
				return NativeType.VOID;
			}
		} else {
			return val;
		}
	}
}
